package com.faceye.core.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.faceye.core.service.BaseService;
import com.faceye.core.util.bean.BeanMapper;
import com.faceye.core.util.result.PageResponse;
import com.faceye.core.util.result.PageResult;
import com.google.common.collect.Lists;

public class  BaseApi<DTO, ID, S extends BaseService>{
	protected Logger logger = LoggerFactory.getLogger(getClass());
	protected S service = null;// 基础服务


	public BaseApi(S service) {
		this.service = service;
	}
	
	

	/**
	 * 对页面对像进行转换
	 * 
	 * @param source
	 * @param destinationClass
	 * @return
	 */
	protected <T> PageResponse<T> mapperPage(PageResult source, Class<T> destinationClass) {
		PageResponse<T> res = new PageResponse<T>();
		if (source != null) {
			res.setPage(source.getPage());
			res.setSize(source.getSize());
			res.setTotal(source.getTotal());
			res.setTotalPage(source.getTotalPage());
			// res.setItems(toResponses(pageResponse.getItems()));
			res.setItems(BeanMapper.mapList(source.getItems(), destinationClass));
		}
		return res;
	}

	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	protected Boolean doMultiRemove(String ids) {
		Boolean res = false;
		try {
			List<Long> idList = Lists.newArrayList();
			String[] idArray = StringUtils.split(ids, ",");
			for (String id : idArray) {
				if (StringUtils.isNotEmpty(id)) {
					idList.add(NumberUtils.toLong(id));
				}
			}
			res = this.service.remove(idList.toArray(new Long[idList.size()]));
		} catch (Exception e) {
			logger.error(">>FaceYe throws Exception when do multi remove in base api.{}", e);
		}
		return res;
	}
}
